//
//  UserInfo.swift
//  XiaoJingMallSwift
//
//  Created by beisheng03 on 2017/7/22.
//  Copyright © 2017年 beisheng. All rights reserved.
//

import UIKit

class UserInfo: NSObject ,NSCoding{
    
    
    var username = " "
    var jp_alias = " "
    var userid = " "
    var key = " "

    static let filePath = "accout.plist".cacheDir()
    
    init(dic : [String : AnyObject]) {
        super.init()
        
        setValuesForKeys(dic)
        
    }
    

    
    override func setValue(_ value: Any?, forUndefinedKey key: String) {
        
        
    }
    
    
    //在swift中想要打印对象要以下重写属性
    override var description: String{
        let keys = ["username","jp_alias","userid","key"]
        let dic = dictionaryWithValues(forKeys: keys)
        return "\(dic)"
    }
    
    
    
    
    required init?(coder aDecoder: NSCoder) {
        
        
        username = aDecoder.decodeObject(forKey: "username") as! String
        jp_alias = aDecoder.decodeObject(forKey: "jp_alias") as! String
        userid = aDecoder.decodeObject(forKey: "userid") as! String
        key = aDecoder.decodeObject(forKey: "key") as! String

        
    }
    
    
    func encode(with aCoder: NSCoder) {
        
        aCoder.encode(username, forKey: "username")
        aCoder.encode(jp_alias, forKey: "jp_alias")
        aCoder.encode(userid, forKey: "userid")
        aCoder.encode(key, forKey: "key")
        
       
        
        
    }
    
    
    //保存用户信息
    func saveUserInfo(){
        
        _ =  NSKeyedArchiver.archiveRootObject(self, toFile:UserInfo.filePath)
        
    }



}
